package com.icode.web.controller;

import com.icode.core.dto.manager.ContactsDTO;
import com.icode.core.dto.manager.MessageModelDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.manage.Contacts;
import com.icode.core.model.manage.ModelTemplate;
import com.icode.core.shared.ValidateNumHandle;
import com.icode.service.house.HouseService;
import com.icode.service.user.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-2-12
 * Time: 下午2:20
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/note")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "contacts", method = RequestMethod.GET)
    public ModelAndView contacts(String contactsGuid) {
        if ("".equals(contactsGuid) || contactsGuid == null) {
            return new ModelAndView("back/note/contacts", "contactsDTO", new ContactsDTO());
        }
        ContactsDTO contactsDTO = messageService.findContactsByGuid(contactsGuid);
        return new ModelAndView("back/note/contacts", "contactsDTO", contactsDTO);
    }

    @RequestMapping(value = "contacts/form", method = RequestMethod.POST)
    public String contactsForm(ContactsDTO contactsDTO) {
        messageService.saveOrUpdateContacts(contactsDTO);
        return "redirect:/note/contacts/list/1";
    }

    @RequestMapping(value = "contacts/list/{currentPage}", method = RequestMethod.GET)
    public String show(@PathVariable String currentPage, Model model) {
        String totalPage = houseService.findTotalPage(Contacts.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<ContactsDTO> contactsDTOs = messageService.findContacts(currentPage);
        List<MessageModelDTO> messageModelDTOs = messageService.findMessageModelByName(ModelTemplate.CUSTOMER1);
        List<MessageModelDTO> messageModelByName = messageService.findMessageModelByName(ModelTemplate.CUSTOMER2);
        messageModelDTOs.add(messageModelByName.isEmpty() ? null : messageModelByName.get(0));
        List<MessageModelDTO> modelSecond = messageService.findMessageModelByName(ModelTemplate.CONTRACT);
        List<MessageModelDTO> modelNotice = messageService.findMessageModelByName(ModelTemplate.NOTIFY);

        model.addAttribute("messageModelDTOs", messageModelDTOs);
        MessageModelDTO attributeValue = modelSecond.isEmpty() ? null : modelSecond.get(0);
        model.addAttribute("modelSecond", attributeValue);
        model.addAttribute("contactsDTOs", contactsDTOs);
        MessageModelDTO attributeValue1 = modelNotice.isEmpty() ? null : modelNotice.get(0);
        model.addAttribute("modelNotice", attributeValue1);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "back/note/contactsOverView";
    }

    @RequestMapping(value = "contacts/update/{contactsGuid}", method = RequestMethod.GET)
    public ModelAndView update(@PathVariable String contactsGuid) {
        ContactsDTO contactsDTO = messageService.findContactsByGuid(contactsGuid);
        return new ModelAndView("back/note/contacts", "contactsDTO", contactsDTO);
    }

    @RequestMapping(value = "contacts/del/{contactsGuid}", method = RequestMethod.GET)
    public String delContacts(@PathVariable String contactsGuid) {
        messageService.delContactsByGuid(contactsGuid);
        return "redirect:/note/contacts/list/1";
    }

    @RequestMapping(value = "contacts/delmore/{contactsGuids}", method = RequestMethod.GET)
    public String delmoreContacts(@PathVariable String contactsGuids) {
        if (!"".equals(contactsGuids)) {
            messageService.delmoreContacts(contactsGuids);
        }
        return "redirect:/note/contacts/list/1";
    }

    @RequestMapping(value = "send_notes", method = RequestMethod.POST)
    @ResponseBody
    public Object sendNotes(String guids, String modelGuid, String modelValue, String address, String signTime, String signAddress, String signContact, String signPhone, HttpServletRequest request) {
        return messageService.sendModel(guids, modelGuid, modelValue, address, signTime, signAddress, signContact, signPhone);
    }

    @RequestMapping(value = "checkbalance", method = RequestMethod.GET)
    @ResponseBody
    public String checkBalance() {
        String str = ValidateNumHandle.checkBalance();
        String[] array = str.split("&");
        String balance = array[1].substring(8);
        return balance;
    }

    @RequestMapping(value = "sendgroup", method = RequestMethod.POST)
    public String sendMsg(String phoneNum, String content, String username) {
//        String[] phoneNums = phoneNum.split(",");
//        String[] usernames = username.split(",");
//        for (int i = 0; i < phoneNums.length; i++) {
//            String str = ValidateNumHandle.sendNote(phoneNums[i], content, usernames[i]);
//            System.out.println(str);
//        }
        return "front/sendMessages";
    }

    @RequestMapping(value = "customer", method = RequestMethod.POST)
    @ResponseBody
    public String customer(String phoneNum, String custProduct, String custPaid, String custSurplus, String custAddress) {
        return messageService.sendToCustomer(phoneNum, custProduct, custPaid, custSurplus, custAddress);
    }

    @RequestMapping(value = "service", method = RequestMethod.POST)
    @ResponseBody
    public String service(String phoneNum, String serviceCustomerName, String gender, String serviceProduct, String servicePaid, String serviceCustomerTel) {
        return messageService.sendToService(phoneNum, serviceCustomerName, gender, serviceProduct, servicePaid, serviceCustomerTel);
    }

    @RequestMapping(value = "model_show", method = RequestMethod.GET)
    public ModelAndView showMessageModel() {
        List<MessageModelDTO> messageModelDTOs = messageService.findAllMessageModel();
        return new ModelAndView("back/note/messageModelOverView", "messageModelDTOs", messageModelDTOs);
    }

    @RequestMapping(value = "model_save", method = RequestMethod.POST)
    public String messageModelSave(MessageModelDTO messageModelDTO) {
        messageService.messageModelSaveOrUpdate(messageModelDTO);
        return "redirect:/note/model_show";
    }

    @RequestMapping(value = "model_before_update", method = RequestMethod.GET)
    public ModelAndView beforeUpdate(String modelGuid, Model model) {
        if (modelGuid == null || "".equals(modelGuid)) {
            MessageModelDTO messageModelDTO = new MessageModelDTO();
            model.addAttribute("templates", MessageModelDTO.initTemplate());
            return new ModelAndView("back/note/messageModelUpdate", "messageModelDTO", new MessageModelDTO());
        }
        MessageModelDTO messageModelDTO = messageService.findMessageModelByGuid(modelGuid);
        return new ModelAndView("back/note/messageModelUpdate", "messageModelDTO", messageModelDTO);
    }

    @RequestMapping(value = "model_del/{modelGuid}", method = RequestMethod.GET)
    public String messageModelDelete(@PathVariable String modelGuid) {
        messageService.messageModelDelete(modelGuid);
        return "redirect:/note/model_show";
    }

}
